// 深拷贝
export const deepClone = <T>(object: T): T => {
    if (Object.prototype.toString.call(object) === "[object Date]") return <T>(<unknown>new Date(object as Date));
    if (typeof object !== "object" || object === null) return <T>object;
    const copy = Array.isArray(object) ? [] : {};
    for (let key in object) (<any>copy)[key] = deepClone(object[key as keyof typeof object]);
    return <T>copy;
};
// 补零
export const zeroAdd = (n: string | number) => {
    return Number(n) < 10 ? "0" + n : n.toString();
};
export const getImageFile = (url: string, fileName: string): Promise<File> => {
    return new Promise((resolve, reject) => {
        const image = new Image();
        image.crossOrigin = "Anonymous";
        image.onload = () => {
            const canvas = document.createElement("canvas");
            canvas.width = image.width;
            canvas.height = image.height;
            const ctx = canvas.getContext("2d");
            if (!ctx) {
                reject(new Error("Failed to get canvas context"));
                return;
            }
            ctx.drawImage(image, 0, 0, image.width, image.height);
            canvas.toBlob(
                (blob) => {
                    if (blob) {
                        const file = new File([blob], fileName, { type: blob.type });
                        resolve(file);
                    } else reject(new Error("Failed to create Blob from canvas"));
                },
                "image/jpeg",
                1.0
            );
        };
        image.onerror = (error: any) => reject(new Error("Failed to load image: " + error.message));
        image.src = url;
    });
};
// 根据数字周获取其中的天数组
export const getDaysInWeek = (week: number): ("日" | "一" | "二" | "三" | "四" | "五" | "六")[] => {
    let arr: ("日" | "一" | "二" | "三" | "四" | "五" | "六")[] = [];
    if ((week & 0x01) != 0x00) arr.push("日");
    if ((week & 0x02) != 0x00) arr.push("一");
    if ((week & 0x04) != 0x00) arr.push("二");
    if ((week & 0x08) != 0x00) arr.push("三");
    if ((week & 0x10) != 0x00) arr.push("四");
    if ((week & 0x20) != 0x00) arr.push("五");
    if ((week & 0x40) != 0x00) arr.push("六");
    return arr;
};
// 根据天数组获取数字周
export const getWeekIndays = (days: ("日" | "一" | "二" | "三" | "四" | "五" | "六")[]): number => {
    let res = 0;
    if (days.includes("日")) res += 0x01;
    if (days.includes("一")) res += 0x02;
    if (days.includes("二")) res += 0x04;
    if (days.includes("三")) res += 0x08;
    if (days.includes("四")) res += 0x10;
    if (days.includes("五")) res += 0x20;
    if (days.includes("六")) res += 0x40;
    return res;
};
export const hexStringToBytes = (str: string) => {
    let tm = [];
    for (let i = 0; i < str.length / 2; i++) tm[i] = parseInt(str.substring(2 * i, 2 * i + 2), 16);
    return tm;
};
export const cbxStringToInt = (str: string) => {
    if (!str) return 0;
    let tm = str.split(" ");
    let rst = 0;
    for (let i = 0; i < tm.length; i++) rst |= 1 << parseInt(tm[i]);
    return rst;
};
export const cbxStringToByteArray = (str: string) => {
    let rst = cbxStringToInt(str);
    let tm = [];
    for (let i = 0; i < 4; i++) {
        tm[i] = rst & 0xff;
        rst >>= 8;
    }
    return tm;
};
export const cbxStringToIntArray = (str: string) => {
    let tn: number[] = [];
    let tm = [];
    if (str) tm = str.split(" ");
    else return tn;
    for (let i = 0; i < tm.length; i++) tn[i] = parseInt(tm[i]);
    return tn;
};
export const intArrayTocbxString = (a1: number, a2: number, a3: number, a4: number) => {
    var str = "";
    var i = 0;
    for (i = 0; i < 8; i++) {
        if (a1 & 0x01) str += i + " ";
        a1 >>= 1;
    }
    for (i = 0; i < 8; i++) {
        if (a2 & 0x01) str += 8 + i + " ";
        a2 >>= 1;
    }
    for (i = 0; i < 8; i++) {
        if (a3 & 0x01) str += 16 + i + " ";
        a3 >>= 1;
    }
    for (i = 0; i < 8; i++) {
        if (a4 & 0x01) str += 24 + i + " ";
        a4 >>= 1;
    }
    if (str.length > 0) str = str.substring(0, str.length - 1);
    return str;
};
export const bytetou16 = (src: number[], index: number) => {
    var rst = 0;
    rst = src[index];
    rst <<= 8;
    rst += src[index + 1];
    return rst;
};
export const u16tobyte = (data: number) => {
    var dst = new Array(2);
    dst[0] = (data >> 8) & 0xff;
    dst[1] = (data >> 0) & 0xff;
    return dst;
};
export const toHexString = (array: number[], str1: string = "") => {
    var str = "";
    for (var i = 0; i < array.length; i++) {
        var string = array[i].toString(16) + (i < array.length ? str1 : "");
        if (string.length < 2) {
            string = "0" + string;
        }
        str += string.toUpperCase();
    }
    return str;
};
export const strAddsplice = (data: string, str: string = "") => {
    var result = "";
    for (let i = 0; i < data.length; i++) {
        result += data[i];
        if (i % 2 === 1) result += str;
    }
    return result;
};
export const bytesToShort = (tm: number[], begin: number) => {
    if (tm.length <= begin + 1) return 0;
    let rst = tm[begin];
    rst <<= 8;
    rst |= tm[begin + 1];
    return rst;
};
